package com.fyy.socket.tcp.thread;

import java.io.DataOutputStream;
import java.io.ObjectInputStream;
import java.net.Socket;

/**
 * @Author fyy
 * @Description
 * @Date 2020/12/11 9:55
 */
public class LoginThread extends Thread {

    private Socket socket;

    public LoginThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try ( //资源自动关闭，不需要手动关闭
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
             ){
            //接收数据
            User user = (User) ois.readObject();
            System.out.println("用户名:" + user.getUserId());
            System.out.println("密码:" + user.getPassword());
            //响应请求
            if(user.getUserId().equals("admin") && user.getPassword().equals("123")){
                dos.writeUTF("用户登录成功！");
            }else{
                dos.writeUTF("用户名/密码错误！");
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
